package com.talent.core.protocol.mqtt.codec.decoder;

import com.talent.core.protocol.mqtt.enums.TlMqttQos;
import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.request.TlMqttPubCompReq;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPubCompVariableHead;
import io.netty.buffer.ByteBuf;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: QOS2的消息接受者发给发送者的报文确定解码器
 */
public class TlMqttPubCompDecoder {

    public TlMqttPubCompReq build(ByteBuf buf, int type, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader( type, remainingLength);
        int qos = fixedHead.getQos();
        TlMqttQos tlMqttQos = TlMqttQos.valueOf(qos);
        TlMqttPubCompVariableHead variableHead = decodeVariableHeader(buf,tlMqttQos);
        return new TlMqttPubCompReq(fixedHead,variableHead);

    }

    TlMqttFixedHead decodeFixedHeader(int type,int remainingLength) {
        TlMqttFixedHead fixedHead =new TlMqttFixedHead();
        int messageType = type >> 4;
        fixedHead.setMessageType(messageType);
        fixedHead.setType(type);
        fixedHead.setLength(remainingLength);
        return fixedHead;
    }

    TlMqttPubCompVariableHead decodeVariableHeader(ByteBuf buf, TlMqttQos qos) {
        TlMqttPubCompVariableHead variableHead=new TlMqttPubCompVariableHead();
        int messageId = buf.readUnsignedShort();
        variableHead.setMessageId(messageId);
        return variableHead;
    }



}
